//
//  MapManager.m
//  BiuWeather
//
//  Created by 楼楚 on 16/2/29.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "MapManager.h"
#import <CoreLocation/CoreLocation.h>

//#import "GetLocation.h"

@interface MapManager ()<CLLocationManagerDelegate>{
    
    CLGeocoder *_geocoder;
}


@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong ,nonatomic) CLLocation *location;
/*
 HeWeather data service 3.0
 1. hourly_forecast   每小时天气预报
 2.status
 3.daily_forecast  天气预报
 4.aqi 空气质量指数
 5.basic   城市基本信息
 6.suggestion    生活指数
 7.now   实况天气
 */

@end



@implementation MapManager

- (void)start
{
    _locationManager = [[CLLocationManager alloc]init];
    _locationManager.delegate = self;
    
    if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        
        [_locationManager requestWhenInUseAuthorization];
    }
    
    [_locationManager startUpdatingLocation];
}
// Location Manager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    
    [manager stopUpdatingLocation];
    
    if (_delegate && [_delegate respondsToSelector:@selector(mapManager:didUpdateAndGetLastCLLocation:)]) {
        
        _location = [locations lastObject];
      
        [self getAddressByLatitude:_location.coordinate.latitude longitude:_location.coordinate.longitude];
        [_delegate mapManager:self didUpdateAndGetLastCLLocation:_location];
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    
    NSLog(@"定位失败");
    
    if ([CLLocationManager locationServicesEnabled] == NO) {
        
        NSLog(@"定位功能关闭");
        if (_delegate && [_delegate respondsToSelector:@selector(mapManagerServerClosed:)]) {
            
            [_delegate mapManagerServerClosed:self];
        }
        
    } else {
        
        NSLog(@"定位功能开启");
        if (_delegate && [_delegate respondsToSelector:@selector(mapManager:didFailed:)]) {
            
            NSLog(@"%@", error);
            [_delegate mapManager:self didFailed:error];
        }
    }
}


-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
    //反地理编码
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        _placemark=[placemarks firstObject];
        NSLog(@"详细信息:%@",_placemark.addressDictionary);
    }];
}

- (void) getPlace
{
    NSDictionary *dicf = _placemark.addressDictionary;
    NSLog(@"--------------%@",dicf);
}

@synthesize authorizationStatus = _authorizationStatus;

- (CLAuthorizationStatus)authorizationStatus {
    
    return [CLLocationManager authorizationStatus];
}


@end
